package java.wxw;

import javax.sound.midi.Soundbank;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
 * @author wangxianwei
 * @create 2020--06--19--14:37
 */
//创建文件
public class FileCreate extends Final{
    private int filePA,i;
    private boolean flag = true;
    private String fileName,string,string1,string2;
    private int fileMode;
    private int findFilePANo;  //找出物理地址号
    FileExist fileExist = new FileExist();
    FindPANo findPANo = new FindPANo();
    DirExist dirExist = new DirExist();
    FileWrite1 fileWrite1 = new FileWrite1();
    public void fileCreate() throws IOException {
        //如果userName和当前目录不同则不能创建
        if(dirName.toUpperCase().equals(userName.toUpperCase())){
            System.out.println("Error!You must create file in your own dir");
        }else{ //可以创建
            System.out.println("Please input FileName:");
            Scanner in = new Scanner(System.in);
            fileName = in.next(); //文件名
            //如果文件号已存在
            if(fileExist.fileExsit(fileName)>=0){
                System.out.println("Error!Name "+fileName+" has already existed");
                return;
            }else{//文件号还未存在
                System.out.println("Please input FileMode(0-Read Only,1-Write Only,2-Read and Write,3-Protect):");
                System.out.println("Please input FileName:");
                Scanner in1 = new Scanner(System.in);
                fileMode = in1.nextInt();
                if(fileMode==0||fileMode==1||fileMode==2||fileMode==3){
                    findFilePANo=findPANo.findPANo();  //找出新分配的物理地址号
                    if(findFilePANo>=0){
                        i=dirExist.dirExist(userName); //贩毒案用户是否存在，返回是第几个
                        userDirs[i].userFile[fileCount[i]].fileName=fileName;  //将这个新的文件名写入
                        userDirs[i].userFile[fileCount[i]].filePANo=findFilePANo; //将新分配的物理地址号写入
                        userDirs[i].userFile[fileCount[i]].fileMode=fileMode;
                        openIf[i][fileCount[i]].openIf=0; //初始化openIf模式
                        openIf[i][fileCount[i]].openMode=4;
                        string="c:\\osfile\\file\\file";
                        string1=String.valueOf(fileMode); //fileMode转为字符串
                        string=string+string1;
                        FileWriter writer = new FileWriter(string);  //以二进制写的形式打开以str为目录的文件，没有就创建，有就清空内容
                        writer.write("wb");
                        writer.close();
                        fileCount[i]++;  //第i个用户的文件数+1
                        while(flag){
                            System.out.println("Input text now(Y/N)");
                            Scanner in2 = new Scanner(System.in);
                            string2 = in2.next();
                            userDirs[i].userFile[fileCount[i]-1].fileLenghth=0;
                            if(string2.equals("Y")){  //输入文本
                                FileWriter writer1 = new FileWriter(string);  //以二进制写的形式打开以str为目录的文件，没有就创建，有就清空内容
                                writer.write("wb+");
                                writer.close();
                                userDirs[i].userFile[fileCount[i]-1].fileLenghth = fileWrite1.fileWrite1();//调用fileWrite1（）写文件，返回值是文件长度
                                flag = false;
                            }else{      //不输入文本
                                flag=false;
                            }

                        }
                        System.out.println(fileName+"has been created successfully!");
                    }else{ //无分配的地址号（返回的-1）
                        System.out.println("Fail!No Disk Space.Please format your disk.");
                        return;
                    }
                }else{ //选择0-3外
                    System.out.println("Error!FileMode's Range is 0-3n");
                    return;
                }
            }
        }
    }

}
